import java.util.Stack;

public class SingleLinkedList {

    static class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;//表示存储当前头结点的引用

    public void display() {
        Node cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }

    }
    //头插法
    public void addFirst(int data){

        Node node = new Node(data);
        node.next = head;
        head = node;

    }

    //尾插法
    public void addLast(int data){

        Node node = new Node(data);
        if(head ==null) {
            head = node;
        }else {
            Node cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }



    }
    public void checkIndex(int index) {
        if(index < 0 || index > size()) {
            throw new indexNotLegalException("下标不合法!");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        if(index > 0 && index < size()) {
            Node node = new Node(data);
            Node cur = head;
            int count = index - 1;
            while(count != 0) {
                cur = cur.next;
                count--;
            }
            Node pre = cur.next;
            node.next = cur.next;
            cur.next = node;
        }

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = head;
        Node node = null;
        if(head.val == key){
            head = head.next;
        }else {
            while(cur.val != key) {
                node = cur;
                cur = cur.next;
            }
            node.next = cur.next;
        }


    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
       Node prev = head;
       Node cur = head.next ;
       while(cur != null) {
           if(cur.val == key) {
               prev.next = cur.next;
               cur = cur.next;
           }else {
               prev = cur;
               cur = cur.next;
           }
       }
        if(head.val == key) {
            head = head.next;
        }

    }
    //得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }

        return count;
    }

    public void clear(){

        this.head = null;
    }

    //递归逆序打印链表元素
    public void printList(Node head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.print(head.val + " ");
            return;
        }
        printList(head.next);
        System.out.print(head.val + " ");
    }
    //非递归打印
    public void printList() {
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        while(cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.empty()) {
            Node top = stack.pop();
            System.out.print(top.val + " ");
        }
    }

    //
}
